const net = require("net");
const readline = require("readline");

const client = net.Socket();
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: ">发送：",
});

//连接服务
client.connect(9090, "localhost", () => {
  client.write("hello server");
});

//接受消息
client.on("data", (msg) => {
  console.log("receive a msg:" + msg.toString());
});

client.on("error", (error) => {
  console.log(error);
});

//发送消息
rl.prompt();
rl.on("line", (msg) => {
  client.write(msg);
  rl.prompt();
});
